package com.lznb.cloud.service.impl;

import com.lznb.cloud.controller.JsonResult;
import com.lznb.cloud.entity.NewsComment;
import com.lznb.cloud.repository.NewsCommentRepository;
import com.lznb.cloud.service.NewsCommentService;
import com.lznb.cloud.utils.SQLUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class NewsCommentServiceImpl implements NewsCommentService {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private NewsCommentRepository newsCommentRepository;

    @Override
    public NewsComment getNewsComment(String commentId) {
        return newsCommentRepository.findOne(commentId);
    }

    @Override
    public JsonResult save(NewsComment newsComment) {
        JsonResult jsonResult=new JsonResult();
        jsonResult.run(jsonResult,newsCommentRepository.save(newsComment));
        return jsonResult;
    }

    @Override
    public JsonResult update(NewsComment newsComment) {
        JsonResult jsonResult=new JsonResult();
        jsonResult.run(jsonResult, newsCommentRepository.saveAndFlush(newsComment));
        return jsonResult;
    }

    @Override
    public JsonResult delete(String commentId) {
        JsonResult jsonResult=new JsonResult();
        newsCommentRepository.delete(commentId);
        return jsonResult;
    }

    @Override
    public Map findByPage(Integer pageIndex, Integer size,NewsComment newsComment) {
        Sort sort = new Sort(Sort.Direction.DESC, "commentId");
        Pageable pageable = new PageRequest(pageIndex, size, sort);
        Specification<NewsComment> specification = this.handleSpeci(newsComment);
        Page<NewsComment> list = newsCommentRepository.findAll(specification,pageable);
        Map map=new HashMap();
        map.put("code",0);
        map.put("msg","");
        map.put("count",list.getTotalElements());
        map.put("data",list.getContent());
        return map;
    }


    /***
    * 考查询条件处理<br>
    * @param newsComment {@link NewsComment} 查询条件实体
    * @return {@link Specification}
    */
    public Specification<NewsComment> handleSpeci(NewsComment newsComment) {
        Specification<NewsComment> specification = new Specification<NewsComment>() {
            @Override
            public Predicate toPredicate(Root<NewsComment> root, CriteriaQuery<?> criteriaQuery, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicates = SQLUtils.CriteriaSQL(root,newsComment,criteriaBuilder);
                return criteriaBuilder.and(predicates.toArray(new Predicate[predicates.size()]));
            }
        };
        return specification;
    }

//    @Override
//    public JsonResult getReadingnumber(String newsId){
//        JsonResult jsonResult = new JsonResult();
//        jsonResult.run(jsonResult,getReadingnumber(newsId));
//        return jsonResult;
//    }
}
